package com.example.final_project.Home.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.final_project.Home.Activity.CarNewsActivity;
import com.example.final_project.Home.entry.CarNews;
import com.example.final_project.R;

import java.util.List;

public class CarNewsAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<CarNews> news;

    public CarNewsAdapter(Context context, List<CarNews> list) {
        this.context = context;
        this.news = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.carkids,parent,false);
        NewsHolder newsHolder = new NewsHolder(view);
        return newsHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        NewsHolder newsHolder = (NewsHolder)holder;
        newsHolder.setData(position);
    }

    @Override
    public int getItemCount() {
        return news.size();
    }

    class NewsHolder extends RecyclerView.ViewHolder {
        public NewsHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(int position){
            CarNews carNews = news.get(position);

            TextView title = itemView.findViewById(R.id.Ntitle_TV);
            title.setText(carNews.getTitle());
            ImageView img1 = itemView.findViewById(R.id.Ncar1_IV);
            Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+carNews.getImg1()).into(img1);
            ImageView img2 = itemView.findViewById(R.id.Ncar2_IV);
            Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+carNews.getImg2()).into(img2);
            TextView author = itemView.findViewById(R.id.Ncar_author);
            author.setText(carNews.getAuthor());

            //小项点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, CarNewsActivity.class);
                    intent.putExtra("name",carNews.getName());
                    context.startActivity(intent);
                }
            });
        }

    }

}
